package com.sino.dao.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sino.bean.Entityrolerelation;
import com.sino.bean.Role;
import com.sino.bean.TblZhuanjiarukuxinxibiao;
import com.sino.bean.Users;
import com.sino.dao.IRoleDAO;

public class RoleDAO extends HibernateDaoSupport implements IRoleDAO {

	public void delRole(int id) {
		this.getHibernateTemplate().delete(this.getRole(id));
	}

	@SuppressWarnings("unchecked")
	public List<Role> listAllRoles() {
		return (List<Role>) this.getHibernateTemplate().find("from Role");
	}

	public void saveRole(Role role) {
		this.getHibernateTemplate().save(role);
	}

	public void updateRole(Role role) {
		this.getHibernateTemplate().update(role);

	}

	public Role getRole(int id) {
		return (Role) this.getHibernateTemplate().load(Role.class, id);
	}

	public void saveEntityrolerelation(Entityrolerelation err) {
		this.getHibernateTemplate().save(err);

	}

	public void delEntityrolerelation(Entityrolerelation err) {
		this.getSession().delete(err);
		this.getSession().flush();

	}

	@SuppressWarnings("unchecked")
	public List<TblZhuanjiarukuxinxibiao> listAllExperts() {
		return (List<TblZhuanjiarukuxinxibiao>) this.getHibernateTemplate()
				.find("from TblZhuanjiarukuxinxibiao tzjb where tzjb.sysShenhezhuangtaima.bm is null");
	}

	public List<Users> listAllUsers() {
		return (List<Users>)this.getHibernateTemplate().find("from Users");
	}

	@SuppressWarnings("unchecked")
	public List<Users> listUsersByQxkw(String QxkwId) {
		return (List<Users>) this.getHibernateTemplate().find(
				"from Users u where u.qxkw.id=?", QxkwId);
	}

	public List<Entityrolerelation> listEntityByRoleAndType(int roleId, int type) {
		String hql = "from Entityrolerelation err where err.role.id=? and err.type=?";
		return (List<Entityrolerelation>) this.getSession().createQuery(hql)
				.setParameter(0, roleId).setParameter(1, type).list();
	}

	public TblZhuanjiarukuxinxibiao getExpertById(int FBh) {
		return (TblZhuanjiarukuxinxibiao) this.getHibernateTemplate().load(
				TblZhuanjiarukuxinxibiao.class, FBh);
	}

	public Users getUserById(int id) {
		return (Users) this.getHibernateTemplate().load(Users.class, id);
	}
}
